/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.mail.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import yhao.infra.feature.mail.ConfigFactory;
import yhao.infra.feature.mail.EmailConfig;
import yhao.infra.feature.mail.core.MailService;
import yhao.infra.feature.mail.core.MailTemplateFactory;
import yhao.infra.feature.mail.core.entity.Email;
import yhao.infra.feature.mail.core.sender.MailSenderDefinition;
import yhao.infra.feature.mail.core.sender.MailSenderPool;


/**
 * DEMO邮件配置类
 *
 * @author yoara
 */
@Import(EmailConfig.class)
public class MailDemoConfiguration {
    public final static String SENDER_POOL_KEY = "TEMPLATE_MAIL_SENDER";

    @Autowired
    private MailService mailService;

    @Bean("commonEmailSender")
    public MailSenderPool commonEmailSender() {
        MailSenderDefinition definition = new MailSenderDefinition("smtp.126.com",
                "yhao.126.com","yhao.com","系统邮件平台");
        return ConfigFactory.makeSenderPool(SENDER_POOL_KEY,definition);
    }

    public void howToInit(){
        MailTemplateFactory.addTemplate(CommonMailTemplateCode.MAIL_COMMON_TEMPLATE,false);
    }

    public void howToUse(){
        Email email = MailTemplateFactory.initMail(CommonMailTemplateCode.MAIL_COMMON_TEMPLATE,"yoara@126.com");
        email.setSubject("标题");
        email.addParam("内容Key","具体内容");
        mailService.send(email);
    }
}
